package com.onstar.cn.weight;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.onstar.cn.common.util.CommonUtil;

/**
 * Created by 6335yz on 2016/6/22.
 */
public class DriveScoreView extends View {
    Paint bgPaint;
    Paint progressPaint;
    Paint scorePaint;
    Paint bottomTextPaint;

    RectF bgRectF;
    RectF progressRectF;
    float HOME_DRIVER_SCORE_VERTICAL;
    float HOME_DRIVER_SCORE_TEXT_HORIZONTAL;
    float HOME_DRIVER_SCORE_TEXT_VERTICAL;

    float HOME_DRIVER_SCORE_BOTTOM_TEXT_HORIZONTAL;
    float HOME_DRIVER_SCORE_BOTTOM_TEXT_VERTICAL;

    float HOME_DRIVER_SCORE_TEXT_SIZE;

    float HOME_DRIVER_SCORE_HORIZONTAL;
    float HOME_DRIVER_SCORE_RADIUS;
    float sweep;
    final float INIT_SCORE = 5;
    String HOME_DRIVER_SCORE_TEXT;
    String score;

    private static final int MARGIN = 20;

    public DriveScoreView(Context context) {
        super(context);
        init();
    }

    public DriveScoreView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        HOME_DRIVER_SCORE_VERTICAL = getResources().getDimension(R.dimen.home_driver_score_vertical);
        HOME_DRIVER_SCORE_HORIZONTAL = getResources().getDimension(R.dimen.home_driver_score_horizontal);
        HOME_DRIVER_SCORE_RADIUS = getResources().getDimension(R.dimen.home_driver_score_radius);
        HOME_DRIVER_SCORE_TEXT_HORIZONTAL = getResources().getDimension(R.dimen.home_driver_score_text_horizontal);
        HOME_DRIVER_SCORE_TEXT_VERTICAL = getResources().getDimension(R.dimen.home_driver_score_text_vertical);
        HOME_DRIVER_SCORE_TEXT_SIZE = getResources().getDimension(R.dimen.home_driver_score_text_size);
        HOME_DRIVER_SCORE_BOTTOM_TEXT_HORIZONTAL = getResources().getDimension(R.dimen.home_driver_score_bottom_text_horizontal);
        HOME_DRIVER_SCORE_BOTTOM_TEXT_VERTICAL = getResources().getDimension(R.dimen.home_driver_score_bottom_text_vertical);
        HOME_DRIVER_SCORE_TEXT = getResources().getString(R.string.home_driver_score_text);
        score = "_ _";
        bgPaint = new Paint();
        bgPaint.setColor(getResources().getColor(R.color.color_drive_score_bg));
        bgPaint.setStrokeWidth(getResources().getDimension(R.dimen.home_driver_score_width));
        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setAntiAlias(true);

        progressPaint = new Paint();
        progressPaint.setColor(getResources().getColor(R.color.color_drive_score_progress));
        progressPaint.setStrokeWidth(getResources().getDimension(R.dimen.home_driver_score_width));
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setAntiAlias(true);

        scorePaint = new Paint();
        scorePaint.setStrokeWidth(getResources().getDimension(R.dimen.home_driver_score_text_width));
        scorePaint.setColor(getResources().getColor(R.color.color_drive_score_progress));
        scorePaint.setTextSize(43);
        scorePaint.setAntiAlias(true);

        bottomTextPaint = new Paint();
        bottomTextPaint.setStrokeWidth(getResources().getDimension(R.dimen.home_driver_score_text_width));
        bottomTextPaint.setColor(getResources().getColor(R.color.color_drive_score_bottom_text));
        bottomTextPaint.setTextSize(CommonUtil.sp2px(getContext(), 12));
        bottomTextPaint.setAntiAlias(true);

        bgRectF = new RectF(HOME_DRIVER_SCORE_HORIZONTAL, HOME_DRIVER_SCORE_VERTICAL, HOME_DRIVER_SCORE_HORIZONTAL + HOME_DRIVER_SCORE_RADIUS, HOME_DRIVER_SCORE_VERTICAL + HOME_DRIVER_SCORE_RADIUS);
        progressRectF = new RectF(HOME_DRIVER_SCORE_HORIZONTAL, HOME_DRIVER_SCORE_VERTICAL, HOME_DRIVER_SCORE_HORIZONTAL + HOME_DRIVER_SCORE_RADIUS, HOME_DRIVER_SCORE_VERTICAL + HOME_DRIVER_SCORE_RADIUS);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        bgRectF.set(MARGIN, MARGIN, getMeasuredWidth() - MARGIN, getMeasuredWidth() - MARGIN);
        progressRectF.set(MARGIN, MARGIN, getMeasuredWidth() - MARGIN, getMeasuredWidth() - MARGIN);
        canvas.drawArc(bgRectF, 160, 220, false, bgPaint);
        canvas.drawArc(progressRectF, 160, sweep == 0 ? 5 : sweep, false, progressPaint);
        drawScore(canvas);
    }

    private void drawScore(Canvas canvas) {
        float width = scorePaint.measureText(score);
        Rect rect = new Rect();
        scorePaint.getTextBounds(score, 0, score.length(), rect);
//        Log.d("drawScore", " width == " + rect.width() + " height = " + rect.height());
        canvas.drawText(score, getMeasuredWidth() / 2 - width / 2, getMeasuredWidth() / 2 + ((float) rect.height()) / 3, scorePaint);
        width = bottomTextPaint.measureText(HOME_DRIVER_SCORE_TEXT);
        bottomTextPaint.getTextBounds(HOME_DRIVER_SCORE_TEXT, 0, HOME_DRIVER_SCORE_TEXT.length(), rect);
        canvas.drawText(HOME_DRIVER_SCORE_TEXT, (float) getMeasuredWidth() / 2 - width / 2, getMeasuredWidth() / 2 + getMeasuredWidth() / 3, bottomTextPaint);
    }

    public void playAnimator(float inputSweep, int inputScore) {
        float sweepAngle = ((float)(inputScore < 0 ? 0 : inputScore) / (float) 100) * 220;
        ValueAnimator animator = ValueAnimator.ofFloat(0, sweepAngle);
        animator.setInterpolator(new DecelerateInterpolator(1));
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                sweep = (Float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
        score = inputScore < 0 ? "_ _" : String.valueOf(inputScore);
        invalidate();
    }

    public void reset() {
        playAnimator(0, -1);
    }
}
